//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CIISCommon.h"

//---------------------------------------------------------------------------

TDateTime __fastcall Now_ms_cleard()
{
  Word Year, Month, Day, Hour, Min, Sec, MSec;
  TDateTime CurrentEvTime;

  CurrentEvTime = Now();
  DecodeDate(CurrentEvTime, Year, Month, Day);
  DecodeTime(CurrentEvTime, Hour, Min, Sec, MSec);
  CurrentEvTime = EncodeDate( Year, Month, Day );
  CurrentEvTime = CurrentEvTime + EncodeTime( Hour, Min, Sec, 0 );

  return CurrentEvTime;
}

//---------------------------------------------------------------------------

TDateTime __fastcall Now_ms()
{
  Word Year, Month, Day, Hour, Min, Sec, MSec;
  TDateTime CurrentEvTime;

  CurrentEvTime = Now();
  DecodeDate(CurrentEvTime, Year, Month, Day);
  DecodeTime(CurrentEvTime, Hour, Min, Sec, MSec);
  CurrentEvTime = EncodeDate( Year, Month, Day );
  CurrentEvTime = CurrentEvTime + EncodeTime( Hour, Min, Sec, MSec );

  return CurrentEvTime;
}

//---------------------------------------------------------------------------

String __fastcall CIISFloatToStr( double d )
{
  int p;
  String s;

  s = FloatToStrF( d, ffExponent, 5, 0 );
  p = s.Pos( DecimalSeparator );
  if( p > 0 ) s[p] = '.';

  return s;
}

//---------------------------------------------------------------------------

double __fastcall CIISStrToFloat( String s )
{
  int p;
  double d;

  p = s.Pos( "." );
  if( p > 0 ) s[p] = DecimalSeparator;
  d = StrToFloat( s );

  return d;
}

//---------------------------------------------------------------------------

TDateTime __fastcall CIISStrToDateTime( String s )
{
  TDateTime t;
  Word Year, Month, Day, Hour, Min, Sec, MSec;
  int p;

  Year = 1899;
  Month = 12;
  Day = 30;
  Hour = 0;
  Min = 0;
  Sec = 0;
  MSec = 0;

  p = s.Pos( "-" );
  if( p > 0 ) Year = StrToInt( s.SubString( 1, p - 1 ) ); s.Delete( 1, p );

  p = s.Pos( "-" );
  if( p > 0 ) Month = StrToInt( s.SubString( 1, p - 1 )); s.Delete( 1, p );

  p = s.Pos( " " );
  if( p > 0 ) Day = StrToInt( s.SubString( 1, p - 1 )); s.Delete( 1, p );

  p = s.Pos( ":" );
  if( p > 0 ) Hour = StrToInt( s.SubString( 1, p - 1 )); s.Delete( 1, p );

  p = s.Pos( ":" );
  if( p > 0 ) Min = StrToInt( s.SubString( 1, p - 1 )); s.Delete( 1, p );

  p = s.Pos( "-" );
  if( p > 0 ) Sec = StrToInt( s.SubString( 1, p - 1 )); s.Delete( 1, p );

  p = s.Length();
  if( p > 0 ) MSec = StrToInt( s.SubString( 1, p)); s.Delete( 1, p );

  t = EncodeDate( Year, Month, Day );
  t = t + EncodeTime( Hour, Min, Sec, MSec );

  return t;
}

//---------------------------------------------------------------------------

String __fastcall CIISDateTimeToStr( TDateTime t )
{
	Word Year, Month, Day, Hour, Min, Sec, MSec;
  String s;

	DecodeDate(t, Year, Month, Day);
	DecodeTime(t, Hour, Min, Sec, MSec);
  s.sprintf("%4d-%02d-%02d %02d:%02d:%02d-%03d",
            Year, Month, Day, Hour, Min, Sec, MSec);

  return s;
}

//---------------------------------------------------------------------------

bool __fastcall CIISSetSysTime( TDateTime t )
{
	Word Year, Month, Day, Hour, Min, Sec, MSec;
	SYSTEMTIME st;

	DecodeDate(t, Year, Month, Day);
	DecodeTime(t, Hour, Min, Sec, MSec);

	GetSystemTime(&st);

	st.wYear = Year;
	st.wMonth = Month;
	st.wDay = Day;
	st.wHour = Hour;
	st.wMinute = Min;
	st.wSecond = Sec;

	if ( SetSystemTime(&st) ) return true;
	else return false;
}

/*
  BOOL SetNewTime(WORD hour, WORD minutes)
  {
			SYSTEMTIME st;
  		char *pc;
  
			GetSystemTime(&st);       // gets current time
			st.wHour = hour;          // adjusts hours
			st.wMinute = minutes;     // and minutes
			if (!SetSystemTime(&st))  // sets system time
					return FALSE;
			return TRUE;
	}
*/



//---------------------------------------------------------------------------

String __fastcall GetProgVersion()
{
  LPTSTR lptstrFilename;
  LPDWORD lpdwHandle;
  DWORD dwHandle;
  DWORD dwLen;
  LPVOID lpData, lpVer;
  LPVOID lpBuffer;
  PUINT puLen;
  String Ver;
  Cardinal Len;

  puLen = &Len;

  lptstrFilename = Application->ExeName.c_str();
  Ver = "";
  
  dwLen = GetFileVersionInfoSize( lptstrFilename, &dwHandle );

  if((lpData  = malloc(dwLen)) == NULL) return Ver;

  if( !GetFileVersionInfo( lptstrFilename, dwHandle, dwLen, lpData) ) return Ver;

	//if( !VerQueryValue( lpData, TEXT("\\StringFileInfo\\041D04E4\\FileVersion"), &lpBuffer, puLen )) return Ver;
	if( !VerQueryValue( lpData, TEXT("\\VarFileInfo\\Translation"), &lpBuffer, puLen )) return Ver;

  if( Len > 0 ) Ver = (char*)lpBuffer;

  free( lpData );

  return Ver;
}
__int64 __fastcall CIIDiskFree( String Path )
{
	Char DrvChar;
	int DrvNo;

	DrvChar = ExtractFileDrive( Path ).UpperCase()[1];
	DrvNo = DrvChar - 'A' + 1;

	return DiskFree( DrvNo );

}

__int64 __fastcall CIITabFileSize( String DBDir, TTable *Tab )
{
	TSearchRec FRec;
	__int64 TotFZ;
	String TabFiles;

	TabFiles = DBDir + "\\" + Tab->TableName + ".*";

	TotFZ = 0;
	if( FindFirst( TabFiles, faAnyFile, FRec ) == 0 )
	{
	 do
		{
			 TotFZ += FRec.Size;

		} while( FindNext( FRec ) == 0 );
	}
	FindClose( FRec );

	return TotFZ;
}





//---------------------------------------------------------------------------
// TCamurPacket
//---------------------------------------------------------------------------

__fastcall TCamurPacket::TCamurPacket()
{
Args = new TCollection(__classid(TDataArg));
}

//---------------------------------------------------------------------------

__fastcall TCamurPacket::~TCamurPacket()
{
delete Args;
}

//---------------------------------------------------------------------------

String __fastcall TCamurPacket::GetArg(int n)
{
for( int i = 0; i < Args->Count; i++ )
  if( ((TDataArg *)Args->Items[i])->Num == n)
    return ((TDataArg *)Args->Items[i])->Val;
return "";
}

//---------------------------------------------------------------------------

String __fastcall TCamurPacket::GetDelArg(int n)
{
String Rslt;
Rslt = "";
for( int i = 0; i < Args->Count; i++ )
  if( ((TDataArg *)Args->Items[i])->Num == n)
  {
    Rslt = ((TDataArg *)Args->Items[i])->Val;
    Args->Delete( i );
    break;
  }  
return Rslt;
}

//---------------------------------------------------------------------------

bool __fastcall TCamurPacket::ArgInc(int n)
{
for( int i = 0; i < Args->Count; i++ )
  if( ((TDataArg *)Args->Items[i])->Num == n)
    return true;
return false;
}
//---------------------------------------------------------------------------

#pragma package(smart_init)
